﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 培训对象，及培训发布表
    /// </summary>
    public class TrainTargetController : ApiController
    {

        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        // POST: api/TrainTarget
        /// <summary>
        /// 培训对象(计划)增加、发布
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainTarget/Add")]
        public IHttpActionResult Add(Model.TrainTarget model)
        {
            log.Info(JsonConvert.SerializeObject(model));
            ResultMsg resultMsg = new ResultMsg(); 
            try
            {
                BLL.TrainTarget bTrainTarget = new BLL.TrainTarget();
                if (!bTrainTarget.Exists(" TPID="+model.TPID.ToString() + " and DepID="+model.DepID.ToString()))
                {
                    if (new IPR_StudyMeetingMrgSys.BLL.TrainTarget().Add(model) > 0)
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                        resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                        resultMsg.Data = "培训计划发布成功！";
                    }
                    else
                    {
                        //返回token信息
                        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                        resultMsg.Data = "培训计划发布失败！";
                    }
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训计划已经发布，请不要重复！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "培训计划发布异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        // POST: api/TrainTarget
        /// <summary>
        /// 培训对象(计划)分页查找
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/TrainTarget/Search")]
        public IHttpActionResult Search(string swhere, int startindex, int endindex)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                bool blExist = false;
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.TrainTarget().GetListByPage(swhere, "TTID", startindex, endindex);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
                    resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
                    resultMsg.Data = "查询结果不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训对象(计划)编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainTarget/Edit")]
        public IHttpActionResult Edit(Model.TrainTarget model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainTarget().Update(model))
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训对象(计划)编辑成功！";
                }
                else
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训对象(计划)编辑失败！";
                }
            }
            catch (Exception exc)
            {

                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "培训对象(计划)编辑时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 培训对象(计划)删除(根据人员工号)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/TrainTarget/Del")]
        public IHttpActionResult Del(Model.TrainTarget model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.TrainTarget().Delete(model.TTID))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "培训对象(计划)删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                    resultMsg.Data = "培训对象(计划)删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "培训对象(计划)删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        // GET: api/TrainTarget
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET: api/TrainTarget/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/TrainTarget
        public void Post([FromBody]string value)
        {
        }

        // PUT: api/TrainTarget/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE: api/TrainTarget/5
        public void Delete(int id)
        {
        }
    }
}
